import Vue from "vue";
import VueRouter from "vue-router";

// 此处做路由懒加载
const Login = () => import("@/views/Login");
const NotFound = () => import("@/views/NotFound")
const Coupon = () => import("@/views/Coupon")
const User = () => import("@/views/User")
const Home = () => import("@/views/Home")
const Shopping = () => import("@/views/Shopping")
const Splash = () => import("@/views/Splash")

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  { path: "*", component: NotFound },
  {
    path:"/",
    name:"splash",
    component: Splash
  },
  {
    path:"/user",
    name:"user",
    component: User,

    children: [{
      path:"coupon",
      name:"coupon",
      component: Coupon
    }]
  },
  {
    path:"/home",
    name:"home",
    component: Home,

    children: []
  },
  {
    path:"/shopping",
    name:"shopping",
    component: Shopping,

    children: []
  }
];

const router = new VueRouter({
  mode: "history",
  routes,
});

router.beforeEach((to, from, next) => {
  //1 获取token 从localStorage

  let isToken = window.localStorage.getItem("tokenLogin");
  if (isToken) {
    next();
  } else {
    if (to.path === "/login") {
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
